package com.clayecloud.module_identitycard;

import android.graphics.Bitmap;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.clayecloud.lib_base.action.ToastAction;
import com.clayecloud.lib_base.utils.StringUtil;
import com.clayecloud.lib_common.apis.HouseApi;
import com.clayecloud.lib_common.apis.IdentitycardApi;
import com.clayecloud.lib_common.utils.HttpUtil;
import com.clayecloud.lib_common.utils.LoginUtil;
import com.clayecloud.lib_common.utils.MessageUtil;
import com.clayecloud.module_identitycard.databinding.ActivityIdcardBinding;
import com.clayecloud.module_identitycard.entity.IDCard;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class IDCardActivityViewModel extends ViewModel implements ToastAction {


    private IdentitycardApi identitycardApi;

    private Bitmap QRCodeBitmap;

    private MutableLiveData<List<IDCard>> IDCardList;

    public Bitmap getQRCodeBitmap() {
        return QRCodeBitmap;
    }

    public void setQRCodeBitmap(Bitmap QRCodeBitmap) {
        this.QRCodeBitmap = QRCodeBitmap;
    }

    public MutableLiveData<List<IDCard>> getIDCardList() {
        return IDCardList;
    }

    public void setIDCardList(MutableLiveData<List<IDCard>> IDCardList) {
        this.IDCardList = IDCardList;
    }

    public void loadData() {
        identitycardApi = HttpUtil.getInstance().create(IdentitycardApi.class);
        MutableLiveData<List<IDCard>> IDCardList = new MutableLiveData<>();
        setIDCardList(IDCardList);
        setQRCodeBitmap();
    }

    private void initCardData(Bitmap QRCodeBitmap){
        List<IDCard> idCards = new ArrayList<>();
        idCards.add(new IDCard("国仕汇小区 - 0501404","202102250501404-1", "刘垚", "2022-03-24 10:52:49", "昌平国仕汇小区4号楼5单元401", QRCodeBitmap));
        IDCardList.setValue(idCards);
    }

    private void setQRCodeBitmap(){
        Integer userID = LoginUtil.getUserID();
        identitycardApi.getUserBindAllHouse(LoginUtil.getUserToken(userID)).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MessageUtil>() {
                    private Disposable disposable;
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(@NonNull MessageUtil messageUtil) {
                        if(messageUtil.getResult()){
                            String QRCode = messageUtil.getObj().toString();
                            String[] splitTemp = QRCode.split(",");
                            Bitmap temp = StringUtil.stringToBitmap(splitTemp[1]);
                            setQRCodeBitmap(temp);
                            initCardData(temp);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        toast("网络错误，请检查网络");
                        Log.e("getUserInfo",e.getMessage());
                        disposable.dispose();
                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });
    }


}
